package com.tjtt.tdm.codeReview.dao.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;

import com.tjtt.tdm.base.BaseHibernateDao;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.codeReview.dao.ICodeReviewDao;
import com.tjtt.tdm.exception.ServiceException;
import com.tjtt.tdm.flow.bean.FlowDetailed;
import com.tjtt.tdm.flow.bean.FlowTask;
import com.tjtt.tdm.flow.bean.FlowTaskInfo;
import com.tjtt.tdm.flow.bean.Flowtype;
import com.tjtt.tdm.form.bean.TdmAnnex;


/**
 * @author 张海娜
 * 
 */
public class CodeReviewDaoImpl extends BaseHibernateDao implements ICodeReviewDao {
	/**
	 * 根据流程实例id获得实体类
	 * @return
	 * @throws IOException
	 * @throws ServiceException String
	 * @author 张海娜
	 * @date 2013-2-25 下午02:16:54
	 */
	public Flowtype findFileNameById(Long id) {
		Flowtype flowtype = (Flowtype)super.getHibernateTemplate().get(Flowtype.class, id);
		return flowtype;
	}
	
	public String saveFlowTask(FlowTask flowTask) {
		return this.getHibernateTemplate().save(flowTask).toString();
	}
	
	public void updateFlowTask(FlowTask flowTask) {
		this.getHibernateTemplate().update(flowTask);
	}

	/**
	 * 
	 * 描述：流程详细信息
	 * @author wangxd
	 * @date Jul 1, 2015 10:51:08 AM
	 * @return void
	 * @throw
	 */
	public void saveFlowDetailed(FlowDetailed flowDetailed) {
		this.getHibernateTemplate().save(flowDetailed);
	}
	
	public FlowTask getFlowTaskById(Long ids) {
		FlowTask flowTask  = (FlowTask) super.getHibernateTemplate().get(FlowTask.class ,ids);
		return flowTask;
	}
	
	public FlowTaskInfo getFlowTaskInfoById(Long ids) {
		FlowTaskInfo flowTaskInfo  = (FlowTaskInfo) super.getHibernateTemplate().get(FlowTaskInfo.class ,ids);
		return flowTaskInfo;
	}
	
	public void updateFlowTaskInfo(FlowTaskInfo flowTaskInfo) {
		this.getHibernateTemplate().update(flowTaskInfo);
	}
	
	public void saveFlowTaskInfo(FlowTaskInfo flowTaskInfo) {
		this.getHibernateTemplate().save(flowTaskInfo);
	}
	public List queryListBySql(String sql) {
		Query query = getSession().createSQLQuery(sql);
		return query.list();
	}
	/**
	 * 
	 *方法描述:查询原始记录模版
	 *@author: wangxd
	 *@date: 2014-3-10下午12:15:11
	 *@param filterMap
	 *@return
	 */
	public PageResult queryFilemodeList(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*) from FILE_MODE t where 1=1 ");
		queryString.append("select ID,FILE_NAME,O.ORG_NAME,FILE_PATH,ROWINDEX,COLINDEX,MODETYPE FROM FILE_MODE t left join SYS_ORGANIZATION o on o.org_id =t.org_name  where t.FILE_PATH is not null ");
        if (filterMap != null && !filterMap.isEmpty()) {
			
			if(null != filterMap.get("filename") &&! "".equals(filterMap.get("filename"))){
				queryString.append(" and FILE_NAME like '%"+filterMap.get("filename")+"%'");
				queryCountString.append(" and FILE_NAME like '%"+filterMap.get("filename")+"%'");
			}
			if(null != filterMap.get("orgId") &&! "".equals(filterMap.get("orgId"))){
				queryString.append(" and t.ORG_NAME = '"+filterMap.get("orgId")+"'");
				queryCountString.append(" and t.ORG_NAME = '"+filterMap.get("orgId")+"'");
			}
			
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord);
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	/**
	 * 
	 *方法描述:查询报告模版
	 *@author: wangxd
	 *@date: 2014-3-10下午12:15:11
	 *@param filterMap
	 *@return
	 */
	public PageResult queryReportFilemodeList(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*) from REPORT_MODE T LEFT JOIN  SYS_ORGANIZATION S  ON T.ORG_NAME =S.ORG_ID  WHERE 1=1");
		queryString.append("SELECT T.ID,T.REPORT_NAME,T.REPORT_PATH,S.ORG_NAME   FROM REPORT_MODE T   LEFT JOIN  SYS_ORGANIZATION S  ON T.ORG_NAME =S.ORG_ID WHERE 1=1 ");
		if (filterMap != null && !filterMap.isEmpty()) {
			if(null != filterMap.get("filename") &&! "".equals(filterMap.get("filename"))){
				queryString.append(" and REPORT_NAME like '%"+filterMap.get("filename")+"%'");
				queryCountString.append(" and REPORT_NAME like '%"+filterMap.get("filename")+"%'");
			}
			if(null != filterMap.get("orgId") &&! "".equals(filterMap.get("orgId"))){
				queryString.append(" and T.ORG_NAME  = "+filterMap.get("orgId"));
				queryCountString.append(" and T.ORG_NAME = "+filterMap.get("orgId"));
			}
			
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord);
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	
	public TdmAnnex queryTdmAnnexById(Long id) {
		TdmAnnex tdmAnnex = (TdmAnnex)super.getHibernateTemplate().get(TdmAnnex.class, id);
		return tdmAnnex;
	}
	/**
	 * 
	 * 描述：查询原始记录对应的标签
	 * @author wangxd
	 * @date Aug 5, 2015 5:16:45 PM
	 * @return List
	 * @throw
	 */
	public List queryFlowItemByFileModeId(Long fileModeId,String bqType)
	{
		List list = null;
		String   sql = "select t.ITEM_NAME,t.form_ename,t.field_ename   from tdm_filemode_item t where t.file_mode_id ="+fileModeId+" and t.bq_type ="+bqType+"  and t.ITEM_NAME is not null and t.form_ename is not null and t.field_ename is not null order by t.id asc ";
		Query query = getSession().createSQLQuery(sql);
		list = query.list();
		return list;
	}
	
	/**
	 * 
	 * 描述：样品接收
	 * @author wangxd
	 * @date Aug 21, 2015 5:02:52 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult queryGwmSampleManageList(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*) from ( ");
		queryString.append("select * from (select t.id,t_weituohao,t_taskname,t.t_weituodanw,u.username,t.t_shenqingrq,t.t_telephone,t.t_taskid,t.t_samplestate," +
				           " case when f.table_name='GWM_WEITUODAN' then (select g1.JIESHOUFANGDAIBIAO  from GWM_WEITUODAN g1 where g1.id = f.main_id) " +
				           " when  f.table_name='GWM_CHANGGIJIANCE' then  (select g2.T_JIESHOUDAIBIAO  from GWM_CHANGGIJIANCE g2 where g2.id = f.main_id) end as jieshoudaibiao " +
				           " from  gwm_sample_manage t  left join flow_task f on t.t_taskid = f.id left join sys_users u on t.t_weituoren = u.userid ) t1 where 1=1 and jieshoudaibiao="+filterMap.get("userId"));
        if (filterMap != null && !filterMap.isEmpty()) {
			
        	if(null != filterMap.get("flag") &&"0".equals(filterMap.get("flag"))){
				queryString.append(" and t1.t_samplestate is null ");
			}
        	else if(null != filterMap.get("flag") &&"1".equals(filterMap.get("flag"))){
        		if(null != filterMap.get("samplestate") &&"0".equals(filterMap.get("samplestate"))){
        			queryString.append(" and t1.t_samplestate =0  ");
        		}else{
        			queryString.append(" and t1.t_samplestate =1  ");
        		}
				
			}
			if(null != filterMap.get("shenqingrq") &&! "".equals(filterMap.get("shenqingrq"))){
				queryString.append(" and t1.t_shenqingrq >='"+filterMap.get("shenqingrq")+"'");
			}
			if(null != filterMap.get("endrq") &&! "".equals(filterMap.get("endrq"))){
				queryString.append(" and t1.t_shenqingrq <='"+filterMap.get("endrq")+"'");
			}
			if(null != filterMap.get("weituodanwei") &&! "".equals(filterMap.get("weituodanwei"))){
				queryString.append(" and t1.t_weituodanw like '%"+filterMap.get("weituodanwei")+"%'");
			}
			if(null != filterMap.get("shibiehao") &&! "".equals(filterMap.get("shibiehao"))){
				queryString.append(" and t1.t_weituohao like '%"+filterMap.get("shibiehao")+"%'");
			}
			if(null != filterMap.get("weituoren") &&! "".equals(filterMap.get("weituoren"))){
				queryString.append(" and t1.username like '%"+filterMap.get("weituoren")+"%'");
			}
			
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord);
		}
        queryCountString.append(queryString.toString()+")");
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	/**
	 * 
	 * 描述：样品接收 --样品详细
	 * @author wangxd
	 * @date Aug 21, 2015 5:02:52 PM
	 * @return PageResult
	 * @throw
	 */
	public PageResult querySampleDetailBytaskId(Map filterMap) {
		StringBuffer queryString = new StringBuffer();
		StringBuffer queryCountString = new StringBuffer();
		queryCountString.append("select count(*) from gwm_sample t where 1=1 ");
		queryString.append("select t.id,t.t_samplename,t.t_sampleno,t.t_jieshounum,t.t_pici,t.t_manufacturer,t.t_jieshouren,t.t_samplechuli,t.t_jieshoutime,t.t_jieshouremark,t.t_guihuantime,t.t_guihuanren,t.t_guihuannum,t.t_lingyongren,t.t_guihuanremark  from gwm_sample t where 1=1 ");
        if (filterMap != null && !filterMap.isEmpty()) {
			
			//样品接收-->样品详细列表
			if("0".equals(filterMap.get("flag"))){
				if(null != filterMap.get("taskId") &&! "".equals(filterMap.get("taskId"))){
					queryString.append(" and t.T_TASKID in ("+filterMap.get("taskId")+")");
					queryCountString.append(" and t.T_TASKID in ("+filterMap.get("taskId")+")");
				}
				queryString.append(" and t.t_sfguihuan is null ");
				queryCountString.append(" and t.t_sfguihuan is null ");
			}
			//样品归还列表
			else if("1".equals(filterMap.get("flag"))){
				if(null != filterMap.get("taskId") &&! "".equals(filterMap.get("taskId"))){
					queryString.append(" and t.T_TASKID in ("+filterMap.get("taskId")+")");
					queryCountString.append(" and t.T_TASKID in ("+filterMap.get("taskId")+")");
				}
				queryString.append(" and t.t_sfguihuan ='0' and t.t_samplechuli='退样'");
				queryCountString.append(" and t.t_sfguihuan ='0' and t.t_samplechuli='退样' ");
			}
			//【样品接收】按钮、【样品归还】按钮
			else if("2".equals(filterMap.get("flag"))||"3".equals(filterMap.get("flag"))){
				if(null != filterMap.get("taskId") &&! "".equals(filterMap.get("taskId"))){
					queryString.append(" and t.T_TASKID in ("+filterMap.get("taskId")+")");
					queryCountString.append(" and t.T_TASKID in ("+filterMap.get("taskId")+")");
				}
				if(null != filterMap.get("sampleIds") &&! "".equals(filterMap.get("sampleIds"))&&!"null".equals(filterMap.get("sampleIds"))){
					queryString.append(" and t.id in ("+filterMap.get("sampleIds")+")");
					queryCountString.append(" and t.id in ("+filterMap.get("sampleIds")+")");
				}
				if("3".equals(filterMap.get("flag"))){
					queryString.append(" and t.t_sfguihuan ='0' and t.t_samplechuli='退样'");
					queryCountString.append(" and t.t_sfguihuan ='0' and t.t_samplechuli='退样' ");
				}
				
			}
			
			
			String sidx = (String) filterMap.get("sidx");
			String sord = (String) filterMap.get("sord");
			queryString.append(" order by " + sidx + " " + sord);
		}
		return searchBySQL(queryCountString.toString(), queryString.toString(), filterMap);
	}
	public void deleteFlowTaskInfoByTaskid(String taskid) {
		List<FlowTaskInfo> flowtaskinfolist = getFlowTaskInfoByTaskid(taskid);
		for(int i=0;i<flowtaskinfolist.size();i++){
			this.getHibernateTemplate().delete(flowtaskinfolist.get(i));
		}
	}
	public List<FlowTaskInfo> getFlowTaskInfoByTaskid(String flowtaskid) {
		List<FlowTaskInfo> itemlists = new ArrayList<FlowTaskInfo>();
		List list = this.getHibernateTemplate().find("from FlowTaskInfo t where t.flowtaskid='"+flowtaskid+"'");
		for(int i=0;i<list.size();i++){
			FlowTaskInfo entity = (FlowTaskInfo) list.get(i);
			itemlists.add(entity);
		}
		return itemlists;
	}
	public List queryTdmPrintreportmodeItem(String fileModeId, String bqType) {
		List list = null;
		String sql = " select t.item_name,t.field_ename,t.form_ename from tdm_printreportmode_item t where t.file_mode_id = "+fileModeId+" and t.bq_type = "+bqType;
		Query query = getSession().createSQLQuery(sql);
		list = query.list();
		return list;
	}
	 /** 
	 *方法描述:根据打印模版ID查询对应的标签
	 *@author: wangxd
	 *@date: 2013-11-15下午02:14:40
	 *@param fileModeId
	 *@return
	 */
	public List queryPrintbiaoqNameById(Long fileModeId,String bqType)
	{
		List list = null;
		String sql = "select t.ITEM_NAME,t.form_ename,t.field_ename   from tdm_printreportmode_item t where t.file_mode_id ="+fileModeId+" and t.bq_type ="+bqType+"  and t.ITEM_NAME is not null and t.form_ename is not null and t.field_ename is not null order by t.id asc ";
		Query query = getSession().createSQLQuery(sql);
		list = query.list();
		return list;
	}
}	
